package com.woate.wingframework.utils;

/**
 * 字符串工具类
 */
public abstract class StringUtils {
    /**
     * 将点转换为反斜杆
     * @param name
     * @return
     */
    public static String dotToSlash(String name){
        char[] chars = name.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            if(chars[i] == '.'){
                chars[i] = '\\';
            }
        }
        return new String(chars);
    }
    /**
     * 将驼峰命名转换为下划线命名
     *
     * @param name
     * @return
     */
    public static String camelToUnderline(String name) {
        if (name == null || "".equals(name.trim())) {
            return "";
        }
        int len = name.length();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = name.charAt(i);
            if (Character.isUpperCase(c) && i > 0) {
                sb.append("_");
                sb.append(Character.toUpperCase(c));
            } else {
                sb.append(Character.toUpperCase(c));
            }
        }
        return sb.toString();
    }

    /**
     * 第一个字母大写
     * @param name
     * @return
     */
    public static String firstCharToUpper(String name){
        if (name == null || "".equals(name.trim())) {
            return "";
        }
        char[] chars = name.toCharArray();
        chars[0] = Character.toUpperCase(chars[0]);
        return new String(chars);
    }
    /**
     * 第一个字母小写
     * @param name
     * @return
     */
    public static String firstCharToLower(String name){
        if (name == null || "".equals(name.trim())) {
            return "";
        }
        char[] chars = name.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }

    /**
     * 下划线格式的命名转换为驼峰命名
     * @param name
     * @return
     */
    public static String underlineToCamel(String name) {
        if (name == null || "".equals(name.trim())) {
            return "";
        }
        int len = name.length();
        StringBuilder sb = new StringBuilder(len);
        name = name.toLowerCase();
        String[] words = name.split("_");
        for (int i = 0; i < words.length; i++) {
            if(i == 0){
                sb.append(words[i]);
            }else{
                sb.append(firstCharToUpper(words[i]));
            }
        }
        return sb.toString();
    }

    /**
     * 填充字符串长度
     * @param in 输入字符串
     * @param rightFillStyle 为真右填充
     * @param fillChar 填充字符
     * @param len 长度
     * @return
     */
    public static String fill(String in, boolean rightFillStyle, char fillChar, int len) {
        String str = in;
        while (str.length() < len) {
            str = rightFillStyle ? str + fillChar : fillChar + str;
        }
        return str;
    }
}
